var express = require('express');
var router = express.Router();
var Recommend = require("../models/recommend");

var multer  = require('multer');
var upload = multer({ dest: 'upload/' });
var shangchuan = require('../models/uplodaTengxun_cos.js');

//获取推荐
router.get('/getRecommend', function(req, res, next) {
    var model = Recommend.find();
    model.exec(function(err,result){
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else{
            res.json({
                status:"0",
                msg:"",
                result:{
                    count:result.length,
                    list:result
                }
            })
        }
    });
});

//添加推荐
router.post('/addRecommend', upload.array("url"),async function(req, res, next) {
    let param = {};

    if(req.files.length != 0){
        param["url"] = await shangchuan(req.files[0]);
    }
    if(req.body.title != ''){
        param["title"] = req.body.title;
    }
    if(req.body.desc != ''){
        param["desc"] = req.body.desc;
    }
    if(req.body.goodsId != ''){
        param["goodsId"] = req.body.goodsId;
    }
    // var param = {
    //     url : await shangchuan( req.files[0]),
    //     title :req.body.title,
    //     desc:req.body.desc,
    //     goodsId :req.body.goodsId,
    // };
    Recommend.create( param ,function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"添加成功",
                    result: "suc"
                })
            }
        }
    })
});

//查找推荐
router.get('/findRecommend', function(req, res, next) {
    Recommend.findOne({_id:req.param("id")},function(err,result) {
        if (err) {
            res.json({
                status: "1",
                msg: err.message,
                result: ""
            })
        } else {
            res.json({
                status:"0",
                msg:"",
                result:{
                    list:result
                }
            })
        }
    })
});

//删除推荐
router.post('/delRecommend', function(req, res, next) {
    Recommend.remove({_id:req.body.id}, function (err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"删除成功",
                    result: "suc"
                })
            }
        }
    });
});


//修改推荐
router.post('/editRecommend', upload.array("url") ,async function(req, res, next) {
    console.log(req.body)
    console.log(req.files)
    let param = {};

    if(req.files.length != 0){
        param["url"] = await shangchuan(req.files[0]);
    }
    if(req.body.title != ''){
        param["title"] = req.body.title;
    }
    if(req.body.desc != ''){
        param["desc"] = req.body.desc;
    }
    if(req.body.goodsId != ''){
        param["goodsId"] = req.body.goodsId;
    }
    Recommend.update({
        _id:req.body.id
    },{
        $set:param
    },function(err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"修改成功",
                    result: "suc"
                })
            }
        }
    });
});


module.exports = router;
